//////////////////////////////////////////////////////////////////////////
// FileName:Grant
// CreateBy:Fenky
// CreateDate:2009-07-17
// Description:
//		
//      
// Modify Log:
//	
// Copyright:ASL-MIS
/////////////////////////////////////////////////////////////////////////

using System;
using System.Data;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Auto.Data.Security;
using BLL = Auto.Data.Systems.BLL;
using Auto.Data.Setting;
using Auto.Data.HumanResource;
using Auto.Web.Common;

namespace BaseSystem._System
{
    public partial class Grant : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitPage();
            }
        }

        #region Init Page
        private void InitPage()
        {

            GVUsers.Attributes.Add("SortExpression", "UserID");
            GVUsers.Attributes.Add("SortDirection", " ASC");
            BindGridView();

            BLL.Systems systems = new BLL.Systems();
            ddlSystems.DataSource = systems.GetSystems();
            ddlSystems.DataTextField = "SysName";
            ddlSystems.DataValueField = "SysID";

            ddlSystems.DataBind();

        }
        #endregion Init Page

        #region Data Bind


        private void BindGridView()
        {
            DataTable dt = new Auto.Data.Security.Users().GetDataByKey(this.txtUserID.Text, "", this.UcDivision1.DivID);
            string sortExpression = this.GVUsers.Attributes["SortExpression"];
            string sortDirection = this.GVUsers.Attributes["SortDirection"];
            if ((!string.IsNullOrEmpty(sortExpression)) && (!string.IsNullOrEmpty(sortDirection)))
            {
                dt.DefaultView.Sort = string.Format("{0} {1}", sortExpression, sortDirection);
            }
            GVUsers.DataSource = dt;
            GVUsers.DataBind();
        }

        #endregion Data Bind

        private void SetNodesCheck()
        {
            SysUserFunc sysUserFunc = new SysUserFunc();
            DataTable sysUserFuncs = new DataTable();
            try
            {
                sysUserFuncs = sysUserFunc.GetDataByKey(ddlSystems.SelectedValue, this.lblUserName.Text, "",SessionBox.User.UserID);

            }
            catch (Exception ex)
            {
                throw ex;
            }

            IDictionary<string, string> checkedFuncNodes = new Dictionary<string, string>();
            foreach (DataRow dr in sysUserFuncs.Rows)
            {
                if (!checkedFuncNodes.ContainsKey(dr[SysUserFunc.FIELD_FuncID].ToString()))
                {
                    checkedFuncNodes.Add(dr[SysUserFunc.FIELD_FuncID].ToString(), dr[SysUserFunc.FIELD_FuncID].ToString());
                }
            }
            ucGranttree1.CheckedFuncNodes = checkedFuncNodes;


            IDictionary<string, string> checkedOperateNodes = new Dictionary<string, string>();
            if (sysUserFunc.SysUsersFuncOperates != null && sysUserFunc.SysUsersFuncOperates.Count > 0)
            {
                foreach (SysUsersFuncOperate funcOperate in sysUserFunc.SysUsersFuncOperates)
                {
                    string key = funcOperate.FuncID + "|" + funcOperate.OperateID;
                    if (!checkedOperateNodes.ContainsKey(key))
                    {
                        checkedOperateNodes.Add(key, funcOperate.OperateID);
                    }
                }
                ucGranttree1.CheckedOperateNodes = checkedOperateNodes;
            }
        }
        /// <summary>
        /// Gets the sys roles sys role func list.
        /// </summary>
        /// <returns></returns>
        private List<SysUsersFuncOperate> GetSysUserFuncOperates()
        {
            List<SysUsersFuncOperate> listOperates = new List<SysUsersFuncOperate>();
            SysUsersFuncOperate sysUsersFuncOperate = null;
            foreach (KeyValuePair<string, string> checkedNode in this.ucGranttree1.CheckedOperateNodes)
            {
                string[] funcOperate = checkedNode.Key.Split('|');
                sysUsersFuncOperate = new SysUsersFuncOperate();
                sysUsersFuncOperate.FuncID = funcOperate[0];
                sysUsersFuncOperate.UserID = lblUserName.Text;
                sysUsersFuncOperate.SysID = ddlSystems.SelectedValue;
                sysUsersFuncOperate.OperateID = funcOperate[1];
                sysUsersFuncOperate.Note = checkedNode.Value;
                listOperates.Add(sysUsersFuncOperate);

            }
            return listOperates;
        }
        #region Event
        protected void GVUsers_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "SetRoles":
                    int rowIndex = Convert.ToInt32(e.CommandArgument);
                    System.Drawing.Color selectedbackcolor = System.Drawing.ColorTranslator.FromHtml("#d7e3f6");
                    for (int i = 0; i < GVUsers.Rows.Count; i++)
                    {
                        if (i == rowIndex)
                        {
                            GVUsers.Rows[i].BackColor = selectedbackcolor;
                        }
                        else
                        {
                            GVUsers.Rows[i].BackColor = System.Drawing.Color.Empty;
                        }
                    }
                    string userID = ((Label)GVUsers.Rows[rowIndex].FindControl("lblUserID")).Text; //GVUsers.Rows[rowIndex].Cells[3].Text;

                    ucGranttree1.TreeInit(ddlSystems.SelectedValue, ddlSystems.SelectedItem.Text);
                    tbRoles.Visible = true;
                    this.lblUserName.Text = userID;
                    SetNodesCheck();
                    break;
            }


        }
        protected void GVUsers_Sorting(object sender, GridViewSortEventArgs e)
        {
            string sortExpress = e.SortExpression.ToString();
            string sortDirection = this.GVUsers.Attributes["SortDirection"].ToString();
            GVUsers.Attributes["SortExpression"] = sortExpress;
            GVUsers.Attributes["SortDirection"] = sortDirection == " DESC" ? " ASC" : " DESC";


            BindGridView();
        }

        protected void lkbtnOK_Click(object sender, EventArgs e)
        {
            string currentUserID = SessionBox.User.UserID;
            IList<SysUserFunc> sysUserFuncs = new List<SysUserFunc>();

            SysUserFunc func = null;
            IDictionary<string, string> checkNodes = ucGranttree1.CheckedFuncNodes;
            foreach (KeyValuePair<string, string> keyValue in checkNodes)
            {
                func = new SysUserFunc();
                func.FuncID = keyValue.Key;
                func.SysID = ddlSystems.SelectedValue;
                func.UserID = lblUserName.Text;
                func.GrantUserID = currentUserID;
                sysUserFuncs.Add(func);
            }
            func = new SysUserFunc();
            func.SysID = ddlSystems.SelectedValue;
            func.UserID = lblUserName.Text;
            func.GrantUserID = currentUserID;
            func.SysUsersFuncOperates = GetSysUserFuncOperates();
            func.SysUserFuncs = sysUserFuncs;
            func.Insert();

            this.tbRoles.Visible = false;
        }


        protected void lkbtnCancel_Click(object sender, EventArgs e)
        {
            this.tbRoles.Visible = false;
        }

        protected void ddlDiv_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindGridView();
        }
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindGridView();
        }

        protected void ddlEmpID_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindGridView();
        }
        #endregion Event
    }
}
